import java.util.Scanner;
import java.util.Random;
import java.util.InputMismatchException;
    public class Test6{
    public static int Target() {
        return new Random().nextInt(100) + 1;
    }
    public static void main(String[] args) {
        final int target = Target();
        int count = 0;
        final Scanner scanner = new Scanner(System.in);
        System.out.println("欢迎来到猜数字游戏");
        while (true) {
            System.out.println("请输入你猜测的数字(1-100)");
            try {
                int guess = scanner.nextInt();
                count++;
                if (guess < 1 || guess > 100) {
                    System.out.println("请输入1-100之间的整数");
                    continue;
                }
                if (guess > target) {
                    System.out.println("猜测的数字过大");
                } else if (guess < target) {
                    System.out.println("猜测的数字过小");
                } else {
                    System.out.println("恭喜你猜对了，您共猜测了" + count + "次");
                    break;
                }
            } catch (InputMismatchException e) {
                System.out.println("输入格式错误，请确认输入的是整数");
                scanner.nextLine(); // 清除无效输入
            }
        }
    }
}